package com.bear.cmcc.NetTestHTTP.ui.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;
import com.bear.cmcc.NetTestHTTP.ui.adapter.ChannelMgrAdapter;

/**
 * 频道管理
 * 
 * @author jqa
 * 
 */
public class ChannelMgrActivity extends AbstractActivity {

	private ListView listView;
	private ChannelMgrAdapter adapter;
	private List<InfoSubBean> mList;
	private boolean isModify = false;//否有修改过订阅状态

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitle(R.string.chlmgr);
		setContentView(R.layout.activity_channelmgr);
		init();
	}

	private void init() {
		loadBottomBar();
		choseBottomItemIndex(2);
		setCloseButton(R.string.top_back, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("isModify", isModify);
				setResult(1, intent);
				finish();
			}
		});
		
		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(listItemClick);
		NetworkTestApp.loadChannelList(this,true);
		mList = NetworkTestApp.getChannelList();
		adapter = new ChannelMgrAdapter(this, mList);
		listView.setAdapter(adapter);
	}
	
    @Override
    protected void onStart()
    {
    	choseBottomItemIndex(2);
    	super.onStart();
    }

	private OnItemClickListener listItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3) {
			
			try{
				for(int i=0;i<mList.size();i++){
					if(i==position){
						InfoSubBean b = mList.get(i);
						if(b.getFlag().equals("订阅")){
							//已订阅状态->取消订阅
							b.setFlag("未订阅");
						}else{
							//未订阅状态->加入订阅
							b.setFlag("订阅");
						}
						isModify = true;
						break;
					}
				}
				adapter.notifyDataSetChanged();
				NetworkTestApp.modifyChannelList(ChannelMgrActivity.this, mList);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};
	
	 @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	        switch (keyCode) {
	            case KeyEvent.KEYCODE_BACK:
	            	Intent intent = new Intent();
					intent.putExtra("isModify", isModify);
					setResult(1, intent);
					finish();
	            default:
	                break;
	        }

	        return false;
	    }
	
	
	
}
